﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaiTu.ConfigInfo;
using BaiTu.ConfigFileManager;

namespace BaiTu.Config
{
    public class DataSettingsConfig : IConfig
    {
        private static bool? _databaseIsInstalled;
        /// <summary>
        /// 判断是否创建了数据库
        /// </summary>
        /// <returns></returns>
        public static bool DataBaseIsInstalled()
        {
            if (!_databaseIsInstalled.HasValue)
            {
                var configInfo = GetConfig();
                _databaseIsInstalled = configInfo != null && !string.IsNullOrWhiteSpace(configInfo.DataConnectionString);
            }
            return _databaseIsInstalled.Value;
        }
        /// <summary>
        /// 将创建数据库的标志置为null
        /// </summary>
        public static void ResetCache()
        {
            _databaseIsInstalled = null;
        }

        private static string _logPath = "";
        /// <summary>
        /// 日志文件路径
        /// </summary>
        public static string LogPath
        {
            get
            {
                var dataSettingsConfig = GetConfig();
                if (dataSettingsConfig != null && !string.IsNullOrWhiteSpace(dataSettingsConfig.LogPath))
                {
                    _logPath = dataSettingsConfig.LogPath;
                }
                return _logPath;
            }
        }
        private static System.Timers.Timer redisConfigTimer = new System.Timers.Timer(Framework.Core.Global.BaiTuGlobal.ConfigTimer);

        private static DataSettingsConfigInfo m_configInfo;

        static DataSettingsConfig()
        {
            m_configInfo = BaiTu.ConfigFileManager.DataSettingsConfigFileManager.LoadConfig();
            redisConfigTimer.AutoReset = true;
            redisConfigTimer.Enabled = true;
            redisConfigTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
            redisConfigTimer.Start();
        }
        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ResetConfig();
        }
        /// <summary>
        /// 重设配置类实例
        /// </summary>
        public static void ResetConfig()
        {
            m_configInfo = DataSettingsConfigFileManager.LoadConfig();
        }
        /// <summary>
        /// 获取配置类实例
        /// </summary>
        /// <returns></returns>
        public static DataSettingsConfigInfo GetConfig()
        {
            if (m_configInfo == null)
            {
                m_configInfo = BaiTu.ConfigFileManager.DataSettingsConfigFileManager.LoadConfig();
            }
            return m_configInfo;
        }

        private static bool _isDataBaseInstalled = false;
        /// <summary>
        /// 判断是否已经安装数据库
        /// </summary>
        public static bool IsDataBaseInstalled
        {
            get
            {
                if (m_configInfo == null)
                {
                    return false;
                }
                return m_configInfo.IsDataBaseInstalled.HasValue == false ? false : m_configInfo.IsDataBaseInstalled.Value;
            }

        }

        private static bool _isLog = false;
        /// <summary>
        /// 是否记录日志信息
        /// </summary>
        public static bool IsLog
        {
            get
            {
                if (m_configInfo == null)
                {
                    return false;
                }
                return m_configInfo.IsLog.HasValue == false ? false : m_configInfo.IsLog.Value;
            }
        }
        private static bool _createNewDatabase = false;
        /// <summary>
        /// 是否强制重新创建数据库
        /// </summary>
        public static bool CreateNewDataBase
        {
            get
            {
                if (m_configInfo == null)
                {
                    return false;
                }
                return m_configInfo.CreateNewDataBase.HasValue == false ? false : m_configInfo.CreateNewDataBase.Value;
            }
        }
        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <param name="configinfo"></param>
        /// <returns></returns>
        public static bool SaveConfig(DataSettingsConfigInfo configinfo)
        {
            DataSettingsConfigFileManager rcfm = new DataSettingsConfigFileManager();
            DataSettingsConfigFileManager.ConfigInfo = configinfo;
            return rcfm.SaveConfig();
        }

        public void SaveConfigInitialize()
        {
            if (GetConfig() == null)
            {
                SaveConfig(new DataSettingsConfigInfo());
            }
        }
    }
}
